package cn.powertime.evaluation.facade.impl;

import cn.powertime.evaluation.Constants;
import cn.powertime.evaluation.annotation.Facade;
import cn.powertime.evaluation.entity.DbqPostings;
import cn.powertime.evaluation.entity.KbKnowledgeBase;
import cn.powertime.evaluation.entity.SysBanner;
import cn.powertime.evaluation.facade.IndexFacade;
import cn.powertime.evaluation.service.CpsCourseService;
import cn.powertime.evaluation.service.DbqPostingsService;
import cn.powertime.evaluation.service.KbKnowledgeBaseService;
import cn.powertime.evaluation.service.SysBannerService;
import cn.powertime.evaluation.service.ZcPolicyService;
import cn.powertime.evaluation.vo.PageVo;
import cn.powertime.evaluation.vo.WebRespIndexCourseListVo;
import cn.powertime.evaluation.vo.WebRespIndexPostingsListVo;
import cn.powertime.evaluation.vo.ZcRespIndexListVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.ListIterator;

@Facade
@Order(2)
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
public class IndexFacadeImpl implements IndexFacade {

    @Autowired
    private CpsCourseService cpsCourseService;
    @Autowired
    private SysBannerService sysBannerService;
    @Autowired
    private DbqPostingsService dbqPostingsService;
    @Autowired
    private ZcPolicyService zcPolicyService;

    @Autowired
    private KbKnowledgeBaseService knowledgeBaseService;

    @Override
    public List<WebRespIndexCourseListVo> courseList(Integer type) {
        return cpsCourseService.indexList(type);
    }

    @Override
    public List<SysBanner> bannerList(Integer ascr) {
        QueryWrapper<SysBanner> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ascr", ascr);
        queryWrapper.eq("status", Constants.STATUS_NORMAL);
        return sysBannerService.list(queryWrapper);
    }

    @Override
    public IPage<WebRespIndexPostingsListVo> postingsList(PageVo pageVo) {
        Page<DbqPostings> page = new Page<>(pageVo.getCurrent(), pageVo.getSize());
        IPage<WebRespIndexPostingsListVo> indexList = dbqPostingsService.indexList(page);
        indexList.getRecords().forEach(item-> {
            Document document = Jsoup.parse(item.getContent());  //  将字符串解析成Document对象
            Elements elements = document.select("img");
            ListIterator<Element> listIterator = elements.listIterator();
            List<String> imgs = Lists.newArrayList();
            while (listIterator.hasNext()){
                imgs.add(listIterator.next().toString());
            }
            item.setImgs(imgs);
        });
        return indexList;
    }

    @Override
    public List<KbKnowledgeBase> knowledgeBaseList(Integer type) {
        QueryWrapper<KbKnowledgeBase> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("status", Constants.STATUS_DISABLED);
        queryWrapper.ne("status", Constants.STATUS_DEL);
        queryWrapper.eq("type", type);
        queryWrapper.orderByDesc("id");
        queryWrapper.last("limit 5");
        return this.knowledgeBaseService.list(queryWrapper);
    }
    @Override
    public List<ZcRespIndexListVo> zcList() {
        return zcPolicyService.zcList();
    }


}
